package fina2.dcs.fi.validator;

import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.naming.InitialContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;

import fina2.dcs.context.ContextHelper;
import fina2.dcs.fis.BankTypeLocal;

@Name("bankTypeValidator")
@Scope(ScopeType.SESSION)
public class BankTypeValidator implements Serializable{

	private Log log = Logging.getLog(getClass());

	private String code;

	private ResourceBundle bundle = null;
	private BankTypeLocal btl = null;
	private InitialContext ic = null;

	public BankTypeValidator() {
		UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
		Locale l = root.getLocale();
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		bundle = ResourceBundle.getBundle("messages", l, cl);
		try {
			ic = ContextHelper.getInitialContext();
			btl = ContextHelper.injectBankTypeLocal(ic);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public void validateCode(FacesContext fc, UIComponent ui, Object value) throws ValidatorException {
		FacesMessage fm = new FacesMessage();
		if (value.toString().trim().length() < 1 || value.toString().length() > 12) {
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			fm.setDetail(bundle.getString("fina2.web.illegal.code.length"));
			throw new ValidatorException(fm);
		} else if (btl.findByCode(value.toString().trim()) != null) {
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			fm.setDetail(bundle.getString("fina2.bank.bankTypeCodeNotUnique"));
			throw new ValidatorException(fm);
		}
	}

	public void validateEditCode(FacesContext fc, UIComponent ui, Object value) {
		FacesMessage fm = new FacesMessage();
		if (!code.trim().equals(value.toString().trim())) {
			if (value.toString().trim().length() < 1 || value.toString().trim().length() > 12) {
				fm.setSeverity(FacesMessage.SEVERITY_ERROR);
				fm.setDetail(bundle.getString("fina2.web.illegal.code.length"));
				throw new ValidatorException(fm);
			}else if (btl.findByCode(value.toString().trim()) != null) {
				fm.setSeverity(FacesMessage.SEVERITY_ERROR);
				fm.setDetail(bundle.getString("fina2.bank.bankTypeCodeNotUnique"));
				throw new ValidatorException(fm);
			}
		}
	}

	public void validateDescription(FacesContext fc, UIComponent ui, Object value) {
		FacesMessage fm = new FacesMessage();
		if (value.toString().trim().length() < 1 || value.toString().trim().length() > 255) {
			fm.setSeverity(FacesMessage.SEVERITY_ERROR);
			fm.setDetail(bundle.getString("fina2.pleaseEnterDescription"));
			throw new ValidatorException(fm);
		}
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
